package com.fenghao.subject.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.fenghao.subject.common.enums.IsDeletedEnum;
import com.fenghao.subject.domain.convert.SubjectLabelConvert;
import com.fenghao.subject.domain.entity.SubjectLabelBO;
import com.fenghao.subject.domain.service.SubjectLabelDomainService;
import com.fenghao.subject.infra.basic.entity.SubjectLabel;
import com.fenghao.subject.infra.basic.entity.SubjectMapping;
import com.fenghao.subject.infra.basic.service.SubjectLabelService;
import com.fenghao.subject.infra.basic.service.SubjectMappingService;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SubjectLabelDomainServiceImpl
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    private final SubjectLabelService subjectLabelService;

    private final SubjectMappingService subjectMappingService;

    /**
     * 新增标签
     */
    @Override
    public Boolean addSubjectLabel(SubjectLabelBO subjectLabelBo) {
        SubjectLabel subjectLabel = new SubjectLabel();
        BeanUtil.copyProperties(subjectLabelBo, subjectLabel);
        subjectLabel.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        try {
            String labelName = subjectLabel.getLabelName();
            Preconditions.checkArgument(StringUtils.isNotBlank(labelName), "标签的名称不能为空");
            String createdBy = subjectLabel.getCreatedBy();
            Preconditions.checkArgument(StringUtils.isNotBlank(createdBy), "创建人不能为空");
        } catch (Exception e) {
            log.error("SubjectLabelDomainServiceImpl:addSubjectLabel:{}", e.getMessage());
            return false;
        }
        int insert = subjectLabelService.insert(subjectLabel);
        return insert > 0;
    }

    /**
     * 删除标签
     */
    @Override
    public Boolean deleteSubjectLabel(SubjectLabelBO subjectLabelBo) {

        SubjectLabel subjectLabel = new SubjectLabel();
        BeanUtil.copyProperties(subjectLabelBo, subjectLabel);


        if (subjectLabel.getId() == null) {
            log.error("id不能为空");
            return false;
        }

        Long subjectLabelId = subjectLabel.getId();
        SubjectLabel queryByIdSubjectLabel = subjectLabelService.queryById(subjectLabelId);
        Integer isDeleted = queryByIdSubjectLabel.getIsDeleted();

        if (IsDeletedEnum.DELETED.getCode() == isDeleted) {
            log.error("id:{},已经被删除", subjectLabelId);
            return false;
        }

        subjectLabel.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        return subjectLabelService.update(subjectLabel) > 0;
    }

    /**
     * 更新标签
     */
    @Override
    public Boolean updateSubjectLabel(SubjectLabelBO subjectLabelBo) {
        SubjectLabel subjectLabel = SubjectLabelConvert.INSTANCE.convertBoToLabel(subjectLabelBo);
        return subjectLabelService.update(subjectLabel) > 0;
    }

    /**
     * 查询分类下标签
     */
    @Override
    public List<SubjectLabelBO> queryLabelCategoryByCategoryId(SubjectLabelBO subjectLabelBo) {
        Long categoryId = subjectLabelBo.getCategoryId();

        SubjectMapping subjectMapping = SubjectMapping.builder()
                .categoryId(categoryId)
                .isDeleted(IsDeletedEnum.UN_DELETED.getCode()).build();

        List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);

        if (CollectionUtils.isEmpty(subjectMappingList)) {
            return Collections.emptyList();
        }

        List<Long> labelIdList = subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList = subjectLabelService.batchQueryById(labelIdList);
        return SubjectLabelConvert.INSTANCE.convertLabelListToLabelBOList(subjectLabelList);
    }


}

